package cate.common.table.mall.row;

import cate.common.annotation.SplitParse;
import cate.common.table.ERow;
import cate.common.table.XSSFRowWrap;
import cate.common.util.SplitParser;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

@NoteClass(value = "商店信息表")
public class MallBaseRow extends ERow {
    @NoteField(value = "商店名称")
    public String mallName;

    @NoteField(value = "商品数量分组配置", detail = "如果不配默认取所有商品")
    public String itemNumGroupStr;

    @NoteField(value = "限购重置周期")
    public String autoCycle;

    @NoteField(value = "免费刷新周期")
    public String freeCycle;

    @NoteField(value = "免费刷新次数上限")
    public int freeTimeMax;

    @NoteField(value = "有偿刷新重置周期")
    public String payCycle;

    @NoteField(value = "有偿刷新次数上限")
    public int payTimeMax;

    @NoteField(value = "刷新资源消耗")
    public String consumeCfg;

    @NoteField(value = "刷新替代道具")
    public String replaceCfg;

    @NoteField(value = "售罄刷新")
    public boolean refreshAfterSale;

    @NoteField(value = "是否开启")
    public boolean enable;

    @Override
    public void parse(XSSFRowWrap xr) {
        this.id = xr.getInteger("id", 0);
        this.mallName = xr.getString("商店名称");
        this.itemNumGroupStr = xr.getString("商品数量分组配置");
        this.autoCycle = xr.getString("限购重置周期");
        this.freeCycle = xr.getString("免费刷新周期");
        this.freeTimeMax = xr.getInteger("免费刷新次数上限", 0);
        this.payCycle = xr.getString("有偿刷新重置周期");
        this.payTimeMax = xr.getInteger("有偿刷新次数上限", 0);
        this.consumeCfg = xr.getString("刷新资源消耗");
        this.replaceCfg = xr.getString("刷新替代道具");
        this.refreshAfterSale = StringUtils.equals("1", xr.getString("售罄刷新", "0"));
        this.enable = StringUtils.equals("1", xr.getString("是否开启", "0"));
    }


    /** 商品数量分组配置 */
    public List<itemGroup> groups;
    /** 商店商品总数量 */
    public int itemNum;

    @Override
    public void decode() {
        if (StringUtils.isNotBlank(itemNumGroupStr)) {
            groups = SplitParser.parse(itemGroup.class, itemNumGroupStr);
            groups.forEach(a -> itemNum += a.num);
        }
    }

    public static class itemGroup {
        @NoteField(value = "分组Id")
        @SplitParse(order = 0)
        public int groupId;

        @NoteField(value = "数量")
        @SplitParse(order = 1)
        public int num;
    }
}
